package ext.generic.part;

import com.ptc.core.components.forms.*;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.netmarkets.util.misc.NmContext;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import java.util.*;
import org.apache.log4j.Logger;
import wt.fc.WTReference;
import wt.fc.collections.WTCollection;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.part.WTProductInstance2;
import wt.session.SessionHelper;
import wt.util.*;
import wt.workflow.work.*;
import wt.part.*;
import wt.fc.*;
import wt.lifecycle.LifeCycleManaged;

/**
 * 选中多个对象来清理流程信息的响应方法类
 */
public class ClearHistoricalIBAForMultiObjFormProcessor extends DefaultObjectFormProcessor
{
	private static final String RESOURCE = "com.ptc.netmarkets.util.utilResource";
    private static final Logger log;
    protected String redirectURL;
    private static boolean VERBOSE=false;
    private static String CLASSNAME= ClearHistoricalIBAForMultiObjFormProcessor.class.getName();

    static 
    {
        try
        {
            log = LogR.getLogger(ext.generic.part.ClearHistoricalIBAForMultiObjFormProcessor.class.getName());
            VERBOSE= (WTProperties.getLocalProperties()).getProperty("ext.generic.part.verbose", false);
        }
        catch(Exception exception)
        {
            throw new ExceptionInInitializerError(exception);
        }
    }

    public ClearHistoricalIBAForMultiObjFormProcessor()
    {
        redirectURL = null;
    }
    /**
     *主要方法
     * @param nmcommandbean
     * @param list
     * @return
     * @throws WTException
     */
    public FormResult doOperation(NmCommandBean nmcommandbean, List list)
        throws WTException
    {
        Vector reviewObjs = new Vector();
        java.util.Locale locale = SessionHelper.getLocale();
        FormResult formresult = new FormResult();
        formresult.setStatus(FormProcessingStatus.SUCCESS);
        
        if(nmcommandbean.getSelected().size() == 0)
        {
            FeedbackMessage feedbackmessage = new FeedbackMessage(FeedbackType.FAILURE, locale, new WTMessage("com.ptc.netmarkets.util.utilResource", "NO_OBJECT_SELECTED", null), null, new LocalizableMessage[] {
                new WTMessage("com.ptc.netmarkets.util.utilResource", "NO_OBJECT_SELECTED", null)
            });
            formresult.addFeedbackMessage(feedbackmessage);
            formresult.setStatus(FormProcessingStatus.FAILURE);
            return formresult;
        }
        
        WTObject contextObj = (WTObject)nmcommandbean.getPrimaryOid().getWtRef().getObject();
        //invoke the clearing funcation
		boolean flag= doClearOperation(nmcommandbean, contextObj);
        super.doOperation(nmcommandbean, list);
        return formresult;
    }

    protected FormResult setRefreshInfo(FormResult formresult, NmCommandBean nmcommandbean, List list)
        throws WTException
    {
        DynamicRefreshInfo dynamicrefreshinfo = new DynamicRefreshInfo();
        dynamicrefreshinfo.setLocation(nmcommandbean.getActionOid());
        dynamicrefreshinfo.setOid(nmcommandbean.getActionOid());
        NmCommandBean _tmp = nmcommandbean;
        dynamicrefreshinfo.setAction("U");
        formresult.addDynamicRefreshInfo(dynamicrefreshinfo);
        return formresult;
    }
    
	/**
	 *清除所选对象的”流程信息“属性值
	 */
	public static boolean doClearOperation(NmCommandBean nmcommandbean, WTObject contextObj) throws WTException
	{
		if(VERBOSE)
			System.out.println(">>>" + CLASSNAME + ".doClearOperation()");
		boolean flag=false;
		
		ArrayList list = nmcommandbean.getSelected();
		if(VERBOSE)
			System.out.println("	"+list.size()+" objects are selected!");
		for(int i=0;i<list.size();i++)
		{
			NmContext nmcontext = (NmContext)list.get(i);
            WTObject wtobject = (WTObject)nmcontext.getTargetOid().getWtRef().getObject();
            if(VERBOSE)
				System.out.println("	Process " + wtobject.getDisplayIdentity()+ ".");
		
			String state= ((LifeCycleManaged)wtobject).getLifeCycleState().toString();
			//对于非”正在工作“状态的随签对象，清除”随签流程“属性值	
			//if(!state.equals("INWORK"))
			{
				wtobject= PartWorkflowUtil.clearHistoricalIBA(wtobject, "revise");
			}
			if(VERBOSE)
				System.out.println("	"+ wtobject.getIdentity()+ " is removed from reviewObjs!");
		}
		if(VERBOSE)
			System.out.println("<<<" + CLASSNAME + ".doClearOperation()--flag=" + flag);
		return flag;
	}
}